package com.sumer.psychology.comment.domain.model;

import com.sumer.psychology.comment.application.command.CreateCommentCommand;

/**
 * 评论创建工厂
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/9/25
 * Time: 14:16
 */
public class CommentFactory {
    private static CommentFactory commentFactory = new CommentFactory();

    public static CommentFactory instance() {
        return commentFactory;
    }

    private CommentFactory() {
    }

    public Comment create(CommentRepository commentRepository, CreateCommentCommand aCreateCommentCommand) {
        Comment aComment = new Comment();
        aComment.initialize(commentRepository.nextIdentity(),
                new CommentHost(aCreateCommentCommand.referenceId()),
                aCreateCommentCommand.content(),
                aCreateCommentCommand.author(),
                CommentType.get(aCreateCommentCommand.commentType()));

        if (aCreateCommentCommand.score() != null) {// 带评分的评论
            return new ScoredComment(
                    aComment,
                    aCreateCommentCommand.score());
        } else if (aCreateCommentCommand.object() != null) {// 对评论的评论
            return new CommentToComment(
                    aComment,
                    aCreateCommentCommand.object());
        } else {// 一般评论
            aComment.apply();
            return aComment;
        }
    }
}
